#include "snake.h"
#include "ui_snake.h"
#include <iostream>
using namespace std;
snake::snake(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::snake)
{
    ui->setupUi(this);
    //设置贪吃蛇的蛇头
    QPoint snake_pos(6,6);
    snakes.push_back(snake_pos);
    //设定起始方向
    dir = 1;
    //定时器每次响应时间
    startTimer(300);
    //初始化食物

        srand(time(NULL));
    for (int i = 0;i < 5; i++) {

        int food_x = rand()%16;
        int food_y = rand()%12;
        food_list.push_back(QPoint(food_x,food_y));
        cout<<"x = "<<food_x<<"y = "<<food_y<<endl;
}
}
snake::~snake()
{
    delete ui;
}
void snake::paintEvent(QPaintEvent *event)
{

    QPainter painter(this);
    QPen pen;
    pen.setColor(Qt::blue);
    pen.setWidth(3);
    painter.setPen(pen);
    QBrush brunsh(Qt::green);
    painter.setBrush(brunsh);
    //画格子
    for (int var = 0; var < rect().width()/WH; ++var) {

        painter.drawLine(QPoint(var*WH,0),QPoint(var*WH,rect().height()));

    }
    for (int var = 0; var < event->rect().height()/WH; ++var) {

        painter.drawLine(QPoint(0,var*WH),QPoint(rect().width(),var*WH));

    }

    //画贪吃蛇
    for (int i = 0;i < snakes.size(); i++) {

        painter.drawEllipse(snakes[i].x()*WH,snakes[i].y()*WH,WH,WH);
    }


    //画食物
    painter.setBrush(Qt::red);
    for (int i = 0;i < food_list.size(); i++) {
        painter.drawRect(food_list[i].x()*WH,food_list[i].y()*WH,WH,WH);
    }
}

void snake::timerEvent(QTimerEvent *event)
{   //身体尾巴的移动
    for (int i = snakes.size()-1;i>0;i--) {
        snakes[i] = snakes[i-1];
    }

    switch (dir) {
    case 1://向上移动

        snakes[0].setY(snakes[0].y()-1);

        break;
    case 2://向下移动
        snakes[0].setY(snakes[0].y()+1);

        break;
    case 3://向左移动
        snakes[0].setX(snakes[0].x()-1);

        break;
    case 4://向右移动
        snakes[0].setX(snakes[0].x()+1);

        break;
    default:
        break;
    }
    //撞到自己会死亡
    for (int i = 1;i<snakes.size();i++) {
        if(snakes[0] == snakes[i])
        {
            hide();
            QMessageBox::about(this,"游戏结束","您已失败");
        }
    }
    //吃掉食物然后生成食物
    for (int i = 0;i < food_list.size(); i++) {
         if(snakes[0] == food_list[i])
         {
             snakes.push_back(food_list[i]);
             food_list.removeAt(i);
             srand(time(NULL));
             int food_x = rand()%16;
             int food_y = rand()%12;
             food_list.push_back(QPoint(food_x,food_y));
             break;
         }
    }

    //撞墙则会死亡游戏结束
    if(snakes[0].x() < 0)
    {
        hide();
        QMessageBox::about(this,"游戏结束","您已失败");
    }
    if(snakes[0].y() < 0)
    {
        hide();
        QMessageBox::about(this,"游戏结束","您已失败");
    }
    if(snakes[0].x() > 15)
    {
        hide();
        QMessageBox::about(this,"游戏结束","您已失败");
    }
    if(snakes[0].y() > 13)
    {

        hide();
        QMessageBox::about(this,"游戏结束","您已失败");
    }
    repaint();
    return;
}

void snake::keyPressEvent(QKeyEvent *event)
{
    if(dir == 1)
    {
        if(event->key() == Qt::Key_Left)
        {
            dir = 3;
        }
        else if(event->key() == Qt::Key_Right)
        {
            dir = 4;
        }
    }
    if(dir == 2)
    {
        if(event->key() == Qt::Key_Left)
        {
            dir = 3;
        }
        else if(event->key() == Qt::Key_Right)
        {
            dir = 4;
        }
     }
     if(dir == 3)
     {
         if(event->key() == Qt::Key_Up)
         {
             dir = 1;
         }
         else if(event->key() == Qt::Key_Down)
         {
             dir = 2;
         }
     }
     if(dir == 4)
     {
         if(event->key() == Qt::Key_Up)
         {
             dir = 1;
         }
         else if(event->key() == Qt::Key_Down)
         {
             dir = 2;
         }
     }
}
